<?php

namespace App\Http\Controllers\V1\Admin;


use App\Models\V1\Community;

use App\Models\V1\Preparatory;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\CommunityServer;
use App\Http\Requests\Admin\PreparatoryServer;


class PreparatoryController extends BaseController
{

    /**
     * 筹备组列表
     * @param CommunityServer $request
     * @return \Illuminate\Http\Response
     */
    public function index(CommunityServer $request)
    {
        $query = Preparatory::query();
        $user=auth('box_admin')->user();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');
        if ($request->filled('nickname')) {
            $query->where('nickname', 'like', "%" . $request->input('nickname', "") . "%");
        }
        if ($request->filled('mobile')) {
            $query->where('mobile', $request->input('mobile'));
        }
        if ($request->filled('community_id')) {
            $query->where('community_id', $request->input('community_id'));
        }
        $query->where('agent_id', $user->id);
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 筹备组小区
     *
     * @param PreparatoryServer $request
     * @return \Illuminate\Http\Response
     */
    public function store(PreparatoryServer $request)
    {

        $request->validate('v1.create');
        $password = bcrypt($request->input('password', '123456'));
        $model = new Preparatory();
        $user=auth('box_admin')->user();
        $model->create([
            'username' => $request->input('username'),
            'nickname' => $request->input('nickname', ""),
            'mobile' => $request->input('mobile', ""),
            'password' => $password,
            'avatar' => $request->input('avatar', ""),
            'expire_time' => $request->input('expire_time'),
            'community_id' => $request->input('community_id'),
            'community_name' => $request->input('community_name'),
            'state' => $request->input('state', 1),
            'agent_id' => $user->id,
            'agent_name' => $user->username,
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 筹备组详情
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo = Preparatory::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 更新筹备组
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $vo = Preparatory::find($id);
        $user=auth('box_admin')->user();
//        $password = bcrypt($request->input('password', '123456'));
        $vo->update([
            'username' => $request->input('username'),
            'nickname' => $request->input('nickname', ""),
            'mobile' => $request->input('mobile', ""),
//            'password' => $password,
            'avatar' => $request->input('avatar', ""),
            'expire_time' => $request->input('expire_time'),
            'community_id' => $request->input('community_id'),
            'community_name' => $request->input('community_name'),
            'state' => $request->input('state', 1),
            'agent_id' => $user->id,
            'agent_name' => $user->username,

        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 删除筹备组
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Preparatory::destroy($id);
        return ajax_output(200, "成功");
    }
}
